package com.example.warehouse.sys.service.impl;

import com.example.warehouse.common.ex.ServiceException;
import com.example.warehouse.common.pojo.vo.PageData;
import com.example.warehouse.common.web.ServiceCode;
import com.example.warehouse.sys.dao.persist.respository.IRoleRepository;
import com.example.warehouse.sys.pojo.entity.SysRole;
import com.example.warehouse.sys.pojo.entity.SysUser;
import com.example.warehouse.sys.pojo.param.AddNewRoleParam;
import com.example.warehouse.sys.pojo.param.SysRoleStandardParam;
import com.example.warehouse.sys.pojo.vo.SysRoleListItemVO;
import com.example.warehouse.sys.service.IRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;

@Slf4j
@Service
public class RoleServiceImpl implements IRoleService {
    @Autowired
    private IRoleRepository roleRepository;
    @Override
    public List<SysRoleListItemVO> list() {
        log.debug("开始执行[显示角色列表]数据业务");
        return roleRepository.list();
    }

    @Value("${warehouse.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;

    public RoleServiceImpl() {
        log.debug("创建业务类对象：RoleServiceImpl");
    }

    @Override
    public void addNew(AddNewRoleParam addNewRoleParam) {
        log.debug("开始执行 [添加角色] 业务");
        String name = addNewRoleParam.getName();
        SysRole sysRole = new SysRole();
        //addNewRoleParam.setEnable(1);
        BeanUtils.copyProperties(addNewRoleParam,sysRole);
        roleRepository.insert(sysRole);
    }

    @Override
    public void deleteById(Long id) {
        log.debug("开始执行 [根据id删除角色] 业务");
        int count = roleRepository.countById(id);
        if (count==0){
            String message = "删除失败,该角色不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }

        roleRepository.deleteById(id);
    }

    @Override
    public void updateById(Long id,SysRoleStandardParam sysRoleStandardParam) {
        log.debug("开始执行 [修改角色参数] 业务");
        SysRoleListItemVO role = roleRepository.roleById(id);
        if (role==null){
            String message = "修改角色失败，尝试修改的类别数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }

        int count = roleRepository.countByNotIdAndName(id, sysRoleStandardParam.getName());
        if (count>0){
            String message = "修改类别详情失败，类别名称已经被占用！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_CONFLICT, message);
        }
        SysRole sysRole = new SysRole();
        BeanUtils.copyProperties(sysRoleStandardParam,sysRole);
        int i = roleRepository.updateById(sysRole);
        if (i!=1){
            String message = "修改类别详情失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_UPDATE, message);
        }
    }

    @Override
    public SysRoleListItemVO getRoleById(Long id) {
        SysRoleListItemVO role = roleRepository.roleById(id);
        if (role==null){
            String message = "未查询到该角色";
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND,message);
        }
        return role;
    }


    @Override
    public PageData<SysRoleListItemVO> list(Integer pageNum) {
        log.debug("开始处理【查询角色列表】的业务，页码：{}", pageNum);
        PageData<SysRoleListItemVO> pageData = roleRepository.list(pageNum, defaultQueryPageSize);
        return pageData;
    }

    @Override
    public PageData<SysRoleListItemVO> list(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询角色列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        PageData<SysRoleListItemVO> pageData = roleRepository.list(pageNum, pageSize);
        return pageData;
    }

    @Override
    public List<SysRole> selectAll() {
        List<SysRole> list = roleRepository.selectAll();
        return list;
    }
}
